package com.zhuliwei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuliwei.constants.SystemConstants;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.CategoryDto;
import com.zhuliwei.domain.entity.Article;
import com.zhuliwei.domain.entity.Category;
import com.zhuliwei.domain.vo.CategoryDataVo;
import com.zhuliwei.domain.vo.CategoryVo;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.exception.SystemException;
import com.zhuliwei.mapper.CategoryMapper;
import com.zhuliwei.service.ArticleService;
import com.zhuliwei.service.CategoryService;
import com.zhuliwei.utils.BeanCopyUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author 朱力维
 * @since 2022-10-08 17:39:03
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询文章表 状态为已发布的文章
        LambdaQueryWrapper<Article> articleCondition = new LambdaQueryWrapper<>();
        articleCondition.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        articleCondition.orderByAsc(Article::getCreateTime);
        List<Article> articleList = articleService.list(articleCondition);
        //获取文章的分类id，并且去重(Set集合)
        Set<String> categoryIds = new HashSet<>();
        articleList.forEach(article -> {
            categoryIds.add(article.getCategoryId());
        });
        //查询分类表
        List<Category> categories = listByIds(categoryIds);
        //封装vo
//        categories.forEach(category -> {
//            if (!SystemConstants.STATUS_NORMAL.equals(category.getStatus())){
//               categories.remove(category);
//            }
//        });
        categories = categories.stream()
                .filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);
    }

    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, SystemConstants.NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    @Override
    public ResponseResult<PageVo> pageCategoryList(Integer pageNum, Integer pageSize, CategoryDto categoryDto) {
        //分页查询
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper();
        lqw.like(StringUtils.hasText(categoryDto.getName()), Category::getName, categoryDto.getName());
        lqw.eq(StringUtils.hasText(categoryDto.getStatus()), Category::getStatus, categoryDto.getStatus());
        Page<Category> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, lqw);
        //封装数据返回
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult delCategoryByIds(List<String> ids) {
        for (String id:ids
             ) {
            // 如果该分类下存在文章则不能删除
            LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Article::getCategoryId,id);
            if (articleService.count(lqw)>0){
                throw new SystemException(AppHttpCodeEnum.CATEGORY_ARTICLE_EXIST);
            }
        }
        removeByIds(ids);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryData() {
        List<Category> categoryList = list();
        CategoryDataVo categoryData = new CategoryDataVo();
        List<HashMap> articleNumList = new ArrayList<>();
        categoryList.forEach(category -> {
            HashMap<String,Object> articleNumMap = new HashMap<>();
            //查出该分类下的文章数
            LambdaQueryWrapper<Article> numCondition = new LambdaQueryWrapper<>();
            numCondition.eq(Article::getCategoryId,category.getId());
            Long articleNum = articleService.count(numCondition);
            articleNumMap.put("name",category.getName());
            articleNumMap.put("value",articleNum.intValue());
            articleNumList.add(articleNumMap);
        });
        categoryData.setArticleNumList(articleNumList);
        return ResponseResult.okResult(categoryData);
    }

}

